package com.shankungfu.util;

import com.shankungfu.MainApplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Toast的工具类，专门用于显示Toast
 * @author WangQing
 * 2013-8-19
 */
public class ToastUtil {
	
	private static Context context;
	private static Toast toast;
	private static TextView chapterNameTV;
	private static TextView percentageTV;

	/**
	 * 统一弹出Toast接口
	 * 
	 * @param context
	 * @param text
	 */
	public static void showMessageText(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}
	
	/**解决弹出多个Toast的问题！
	 *  通用 
	 * wangqing
	 * @param context
	 * @param text
	 * @return
	 */
	public static Toast showToast(Context context, String text) {
		if (ToastUtil.context == context) {
			toast.cancel();
			toast.setText(text);
		} else {
			ToastUtil.context = context;
			toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
		}
		
		return toast;
	}
	
	/**解决弹出多个Toast的问题！
	 *   用于自定义布局
	 * wangqing
	 * @param context
	 * @param text
	 * @return
	 */
	public static Toast showToast(Context context, String text, float persent) {
		if (ToastUtil.context == context) {
			toast.cancel();
			chapterNameTV.setText(text);
			percentageTV.setText((int)(persent * 100) + "%");
		} else {
			ToastUtil.context = context;
			LayoutInflater inflater = LayoutInflater.from(context);
			//自定义布局
//			View view = inflater.inflate(R.layout.book_reading_seekbar_toast, null);
//			chapterNameTV = (TextView) view.findViewById(R.id.chapterName);
//			percentageTV = (TextView) view.findViewById(R.id.percentage);
			
			chapterNameTV.setText(text);
			percentageTV.setText((int)(persent * 100) + "%");
			
			toast= new Toast(context);
//			toast.setGravity(Gravity.BOTTOM, 0, PixelFormat.formatDipToPx(context, 70));
//			toast.setView(view);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		
		return toast;
	}
	
	public static void loadDataFailure(boolean b) {
		if (b)
			showMessageText(MainApplication.context, "没有获取到数据，请稍后重试");
	}
}
